﻿using System;
using System.Xml;
using System.IO;

using ns_FilesManage;

namespace ns_FriendsList
{
    public class c_FriendsListXml
    {
        //存储格式<FRIENDS_LIST><FRIEND><FRIEND_NAME>NAME(好友名字)</FRIEND_NAME><FRIEND_TYPE>TYPE(好友类型)</FRIEND_TYPE></FRIEND></FRIENDS_LIST>
        #region//类属性
        private const String FRIEND = "Friend";
        private const String FRIEND_NAME = "FriendName";
        private const String FRIEND_TYPE = "FriendType";
        private const String FRIENDS_LIST = "FriendsList";
        private XmlDocument _XmlDoc = new XmlDocument();
        public String FriendsListXml
        {
            get { return _XmlDoc.OuterXml; }
        }
        #endregion
        #region      //构造函数
        public c_FriendsListXml()//建立格式头
        {
            XmlDeclaration dec = _XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            _XmlDoc.PrependChild(dec);
            XmlElement ele = _XmlDoc.CreateElement(FRIENDS_LIST);
            ele.IsEmpty = false;
            _XmlDoc.AppendChild(ele);
        }
        public c_FriendsListXml(String friendsListXmlPath )
        {
            _XmlDoc.Load(friendsListXmlPath);
        }
        public c_FriendsListXml(String friendsListXmlString, Boolean IsString)//由字符串创建，IsString为伪参数,区分上面由路径创建
        {
            _XmlDoc.LoadXml(friendsListXmlString);
        }
       
        #endregion
        #region       //方法
        private XmlElement FindFriend(String FriendName)
        {
            try
            {
                XmlNode node_FriendsList = _XmlDoc.SelectSingleNode(FRIENDS_LIST);
                if (node_FriendsList == null) return null;
                //查找节点
                XmlElement ele_Friend = null;
                for (Int32 index = 0; index != -1 && node_FriendsList.ChildNodes.Item(index) != null; index++)
                {
                    XmlElement ele_FriendTemp = (XmlElement)node_FriendsList.ChildNodes.Item(index);
                    for (Int32 i = 0; ele_FriendTemp.ChildNodes.Item(i) != null; i++)
                    {
                        XmlElement ele_FriendName = (XmlElement)ele_FriendTemp.ChildNodes.Item(i);
                        if (ele_FriendName.Name == FRIEND_NAME && ele_FriendName.InnerText == FriendName)
                        {
                            ele_Friend = ele_FriendTemp; //获得好友节点
                            index = -2;//使父循环结束
                            break;
                        }
                    }
                }
                return ele_Friend;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public String GetFriendsListXml()
        {
            return FriendsListXml;
        }
        public Boolean SaveFriendsListXml(String Path)
        {
            try
            {
                s_c_FilesManage.FilesManage.OverWriteFile(Path, _XmlDoc.OuterXml);
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        
        }
        public Boolean AddFriend(String FriendName  ,String FriendType )
        {
            try
            {
                //好友已经存在
                if (FindFriend(FriendName) != null) return false;
                //设置好友名字节点
                XmlElement ele_FriendName = _XmlDoc.CreateElement(FRIEND_NAME);
                ele_FriendName.IsEmpty = false;
                ele_FriendName.InnerText = FriendName;
                //设置好友类型节点
                XmlElement ele_FriendType = _XmlDoc.CreateElement(FRIEND_TYPE);
                ele_FriendType.IsEmpty = false;
                ele_FriendType.InnerText = FriendType;
                //设置好友节点，并将以上两个节点加入到好友节点中
                XmlElement ele_Friend = _XmlDoc.CreateElement(FRIEND);
                ele_Friend.IsEmpty = false;
                ele_Friend.AppendChild(ele_FriendName);
                ele_Friend.AppendChild(ele_FriendType);
                //获得好友列表根结点，将上面建立的好友节点加入到列表中
                XmlNode node_FriendsList = _XmlDoc.SelectSingleNode(FRIENDS_LIST);
                if (node_FriendsList == null) return false;
                node_FriendsList.AppendChild(ele_Friend);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public Boolean RemoveFriend(String FriendName)
        {
            try
            {
                XmlElement ele_Friend = FindFriend(FriendName);
                if (ele_Friend == null) return false;
                else
                {
                    XmlNode node_FriendsList = _XmlDoc.SelectSingleNode(FRIENDS_LIST);
                    if (node_FriendsList == null) return false;
                    node_FriendsList.RemoveChild(ele_Friend);
                    return true;
                }
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public Boolean SetFriendType(String FriendName,String value)
        {
            try
            {
                XmlElement ele_Friend = FindFriend(FriendName);
                if (ele_Friend == null) return false;
                else
                {
                    for (Int32 i = 0; ele_Friend.ChildNodes.Item(i) != null; i++)
                    {
                        XmlElement ele_FriendType = (XmlElement)ele_Friend.ChildNodes.Item(i);
                        if (ele_FriendType.Name ==FRIEND_TYPE )
                        {
                            ele_FriendType.InnerText = value;
                            break;
                        }
                    }
                    return true;
                }
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public Int32 CountFriends()
        {
            try
            {
                XmlNode node_FriendsList = _XmlDoc.SelectSingleNode(FRIENDS_LIST);
                if (node_FriendsList == null) return -1;
                return node_FriendsList.ChildNodes.Count;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
        public Boolean IsFriendExist(String FriendName)
        {
            try
            {
                if (FindFriend(FriendName) == null) return false;
                else return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        #endregion
    }
}